# build stage
FROM node:18 as build-stage

WORKDIR /app

COPY package.json .

RUN npm config set registry https://registry.npmmirror.com/

RUN npm install

COPY . .

RUN npm run build

RUN ls -R /app/dist  # Add this to verify the build output

# production stage
FROM node:18 as production-stage

COPY --from=build-stage /app/dist /app/dist
COPY --from=build-stage /app/package.json /app/package.json
COPY --from=build-stage /app/src/email/template/validate.code.ejs app/src/email/template/validate.code.ejs
COPY --from=build-stage /app/ecosystem.config.js /app/ecosystem.config.js
COPY --from=build-stage /app/.env.production /app/.env.production
COPY --from=build-stage /app/python /app/python
COPY --from=build-stage /app/requirements.txt /app/requirements.txt


# 安装 Python 和 GDAL 依赖项
RUN apt-get update && apt-get install -y \
    gdal-bin \
    libgdal-dev \
    python3-gdal

WORKDIR /app

# 更新包列表并安装Python编译所需的依赖项
RUN apt-get update && apt-get install -y build-essential libssl-dev wget

# 下载Python 3.9源码
RUN wget https://www.python.org/ftp/python/3.9.18/Python-3.9.18.tgz -O python.tgz

# 解压源码
RUN tar -xzf python.tgz

# 编译并安装Python到/usr/local目录，避免与系统自带的Python版本冲突
RUN cd Python-3.9.18 && ./configure --prefix=/usr/local && make altinstall

# 创建一个指向Python 3.9的软链接，方便在命令行中使用python命令
RUN ln -s /usr/local/bin/python3.9 /usr/bin/python

# 安装pip，Python的包管理器
RUN /usr/local/bin/python3.9 -m ensurepip --upgrade

# 下载并安装pip
RUN wget https://bootstrap.pypa.io/get-pip.py -O get-pip.py \
    && /usr/local/bin/python3.9 get-pip.py

# 设置环境变量，将Python和pip的安装路径添加到PATH
ENV PATH="/usr/local/bin:$PATH"


# 使用pip3安装特定版本的setuptools
RUN /usr/local/bin/pip install setuptools==57.5.0

# 安装 Python 依赖
RUN /usr/local/bin/pip install --upgrade pip \
    && /usr/local/bin/pip install -r requirements.txt

RUN npm config set registry https://registry.npmmirror.com/

RUN npm install 

RUN npm install pm2 -g

EXPOSE 3000

CMD ["pm2-runtime", "ecosystem.config.js"]
